package com.bgds.sys.module.controller;

import com.alibaba.fastjson.JSONObject;
import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.pagevo.SeriesVo;
import com.bgds.sys.common.utils.DateUtils;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.influx.service.InfluxService;
import com.bgds.sys.module.entity.DeviceDynamicAttr;
import com.bgds.sys.module.entity.dto.AlarmLogDto;
import com.bgds.sys.module.service.IAlarmLogService;
import com.bgds.sys.module.service.IDeviceDynamicAttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lj
 */
@Slf4j
@RestController
@Api(tags = "侧导板对中监测接口")
@RequestMapping("/sideguide")
public class SideguideController {

    @Autowired
    private IDeviceDynamicAttrService iDeviceDynamicAttrService;
    @Autowired
    private InfluxService influxService;
    @Autowired
    private IAlarmLogService iAlarmLogService;

    @RequestMapping(value = "/getDeviceChartData", method = RequestMethod.GET)
    @ApiOperation(value = "侧导板图形分析数据",notes = "侧导板图形分析数据",httpMethod = "GET",response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceCode", dataType = "String", value = "设备编码"),
            @ApiImplicitParam(name = "startTime",  value = "起始时间"),
            @ApiImplicitParam(name = "endTime",  value = "结束时间"),
    })

    public Result<Map<String,Object>> getDeviceChartData(String deviceCode, Long startTime,Long endTime){
        Map<String,Object> map=new HashMap<>(10);
        List<DeviceDynamicAttr> deviceDynamicAttrs=iDeviceDynamicAttrService.listByDeviceCode(deviceCode,"side-guide");
        List<AlarmLogDto> alarmLogs=iAlarmLogService.listByParam(deviceCode, startTime, endTime);
        List<SeriesVo>  chartData= influxService.queryByForMap(deviceCode,startTime,endTime,deviceDynamicAttrs);
        //计算差值
        Map<Long,List<SeriesVo>> group= chartData.stream().collect(Collectors.groupingBy(SeriesVo::getTime));
        //差值对象集合
        DecimalFormat df = new DecimalFormat("#.00");
        group.forEach((time,seriesVos)->{
            if(seriesVos.size()>=4){
                SeriesVo opera=new SeriesVo();
                Double operationDiff= seriesVos.get(0).getValue()-seriesVos.get(1).getValue();
                opera.setName("操作侧差值");
                opera.setTime(time);
                opera.setValue(Double.valueOf(df.format(operationDiff)));

                SeriesVo tr=new SeriesVo();
                Double trans= seriesVos.get(2).getValue()-seriesVos.get(3).getValue();
                tr.setName("传动侧差值");
                tr.setTime(time);
                tr.setValue(Double.valueOf(df.format(trans)));
                chartData.add(opera);
                chartData.add(tr);
            }else{
                SeriesVo tr=new SeriesVo();
                Double trans= seriesVos.get(0).getValue()-seriesVos.get(1).getValue();
                tr.setName("差值");
                tr.setTime(time);
                tr.setValue(Double.valueOf(df.format(trans)));
                chartData.add(tr);
            }
        });
        map.put("data",chartData);
        map.put("alarmLogs",alarmLogs);
        return new ResultUtil<Map<String,Object>>().setData(map);
    }

}
